//
//  BookInfoViewControllerB.m
//  Unicorn
//
//  Created by xalo on 16/4/30.
//  Copyright © 2016年 bhj_jun. All rights reserved.
//

#import "BookInfoViewControllerB.h"
#import "BookViewControllerC.h"
#import "BookBModel.h"
#import "BookBCell.h"


#define kURLb @"http://law.anywebapps.com/law/api.php"//参数（tasks=lawsClassList & language=0）

@interface BookInfoViewControllerB ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)NSMutableArray *dataSourceArr;

@property (nonatomic,retain)UITableView *tableView;

@end

@implementation BookInfoViewControllerB

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"二级目录";

    [self.view addSubview:self.tableView];
    
    //请求数据调用
    [self requestDataWithUrl:kURLb];
    
    //注册单元格
    [self.tableView registerNib:[UINib nibWithNibName:@"BookBCell" bundle:nil] forCellReuseIdentifier:@"BookBCell"];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - - 懒加载
-(NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        
        _dataSourceArr=[NSMutableArray array];
    }
    return _dataSourceArr;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView=[[UITableView alloc]initWithFrame:self.view.bounds];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
    }
    return _tableView;
}



#pragma mark - - 数据请求  @(12),@"tasks":@"lawsList",@"language":@(0)
-(void)requestDataWithUrl:(NSString *)url
{
    [SingleExE postFetchDataWithUrlString:url paramenters:@{@"tasks":@"lawsList",@"fid":self.passid,@"language":@(0)} success:^(id data)
    {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data
                                                            options:NSJSONReadingAllowFragments error:nil];
//NSLog(@"dicB----->数据请求成功%@",dic);
        NSArray *arr = dic[@"tasks"];
        for (NSDictionary *dicB in arr) {
            
            BookBModel *model = [[BookBModel alloc]init];
            [model setValuesForKeysWithDictionary:dicB];
            [self.dataSourceArr addObject:model];
        }
//NSLog(@"BookBdatasourceArr--%@",self.dataSourceArr);
        [self.tableView reloadData];
        
    } fail:^{
        NSLog(@"dicB----->数据请求失败");
    } view:self.view];
    
}

#pragma mark - - tableView代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BookBCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BookBCell" forIndexPath:indexPath];
    BookBModel *model = self.dataSourceArr[indexPath.row];
    [UIView animateWithDuration:0.5 animations:^{
        
        if (indexPath.row % 2 ==0) {
            CGRect frame        = cell.frame;
            frame.origin        = CGPointMake(0, 0);
            cell.frame          = frame;
            [cell setCellWithModel:model img:@"Nature Blur  (9).JPG"];
        }else
        {
            
            CGRect frame        = cell.frame;
            frame.origin        = CGPointMake(100, 0);
            cell.frame          = frame;
            [cell setCellWithModel:model img:@"alphaimg.jpg"];
        }
    } completion:^(BOOL finished) {
        
        NSLog(@"动画结束....");
    }];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BookBModel *model = self.dataSourceArr[indexPath.row];
    CGRect rect= [model.title boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.tableView.frame), MAXFLOAT)
                                           options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                        attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Symbol" size:18]}
                                           context:nil];
    return CGRectGetHeight(rect)+40;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookBModel *bModel = self.dataSourceArr[indexPath.row];
    BookViewControllerC *bookVCC = [[BookViewControllerC alloc]init];
    
    bookVCC.passid = bModel.bookid;//传值ID
    
    [self.navigationController pushViewController:bookVCC animated:YES];
}


@end
